package net.techfuser.channelorderreport.summary;

import com.alibaba.fastjson.JSONObject;
import net.techfuser.channelorderreport.ChannelOrder;

import java.util.Date;


/**
 * 接单时间订单汇总
 *
 * @Description:
 * @Author 左龙龙
 * @Date 21-5-27
 * @Version 1.0
 **/
public class ConfirmTimeSummary implements Summary {

    ChannelOrder last = null, cur = null;

    /**
     * 判断日期是否为同一天
     *
     * @param d1
     * @param d2
     * @return
     */
    public boolean sameDay(Date d1, Date d2) {
        if (null == d1 || null == d2) {
            return false;
        } else {
            return d1.getYear() == d2.getYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate();
        }
    }

    @Override
    public void summary(JSONObject order) {
        last = cur;

        if (null == order || !order.containsKey("confirm_time")) {
            cur = null;
            return;
        }


        Date confirm_time = order.getDate("confirm_time");
        cur = new ChannelOrder();
        cur.date = confirm_time;

        String channel_keyword = order.getString("channel_keyword");
        String order_status = order.getJSONObject("order_status").getString("center");
        if (!"15".equals(order_status)) {
            cur.qnh_valid_order_num = 1;
            cur.channel_valid_order_num = 1;
            cur.qnh_gmv_value = order.getDoubleValue("total_item_yj");
            cur.qnh_logistics_value = order.getDoubleValue("logistics_value");
            cur.qnh_package_fee = order.getDoubleValue("package_fee");
            cur.qnh_total_sale_value = order.getDoubleValue("total_sale_value");

            switch (channel_keyword) {
                case "MTWM": {
                    // 牵牛花订单原价金额（商品金额+单品优惠总额）+包装费
                    cur.channel_gmv_value = order.getDoubleValue("total_item_yj") + order.getDoubleValue("package_fee");
                    break;
                }
                case "ELEM": {
                    // 牵牛花订单原价金额（商品金额+单品优惠总额）+包装费 + 配送费
                    cur.channel_gmv_value = order.getDoubleValue("total_item_yj") + order.getDoubleValue("package_fee") + order.getDoubleValue("logistics_value");
                    break;
                }
                default: {
                    // 牵牛花订单原价金额
                    cur.channel_gmv_value = order.getDoubleValue("total_item_yj");
                }
            }

        } else {
            if ("JDDJ".equals(channel_keyword)) {
                Date cancel_time = order.getDate("cancel_time");
                // 取消也在这天发生不计算为这天的有效单
                if (sameDay(confirm_time, cancel_time)) {
                    cur.channel_valid_order_num = 0;
                } else {
                    cur.channel_valid_order_num = 1;
                }
            }
        }
    }

    @Override
    public ChannelOrder getChange() {
        if (null == last && cur == null) {
            return null;
        } else if (last == null) {
            return cur;
        } else if (cur == null) {
            cur = new ChannelOrder();
            cur.date = last.date;
        }


        ChannelOrder change = new ChannelOrder();
        change.date = cur.date;
        change.qnh_valid_order_num = cur.qnh_valid_order_num - last.qnh_valid_order_num;
        change.qnh_gmv_value = cur.qnh_gmv_value - last.qnh_gmv_value;
        change.qnh_package_fee = cur.qnh_package_fee - last.qnh_package_fee;
        change.qnh_logistics_value = cur.qnh_logistics_value - last.qnh_logistics_value;
        change.qnh_total_sale_value = cur.qnh_total_sale_value - last.qnh_total_sale_value;
        change.channel_gmv_value = cur.channel_gmv_value - last.channel_gmv_value;
        change.channel_valid_order_num = cur.channel_valid_order_num - last.channel_valid_order_num;
        return change;


    }
}


